package com.ruoyi.cbfa.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.cbfa.domain.InsurancePlan;
import com.ruoyi.cbfa.service.IInsurancePlanService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 参保方案Controller
 * 
 * @author ruoyi
 * @date 2025-05-29
 */
@RestController
@RequestMapping("/cbfa/cbfa")
public class InsurancePlanController extends BaseController
{
    @Autowired
    private IInsurancePlanService insurancePlanService;

    /**
     * 查询参保方案列表
     */
    @PreAuthorize("@ss.hasPermi('cbfa:cbfa:list')")
    @GetMapping("/list")
    public TableDataInfo list(InsurancePlan insurancePlan)
    {
        startPage();
        List<InsurancePlan> list = insurancePlanService.selectInsurancePlanList(insurancePlan);
        return getDataTable(list);
    }
    @PreAuthorize("@ss.hasPermi('cbfa:cbfa:list')")
    @GetMapping("/distinctNames")
    public AjaxResult getDistinctPlanNames() {
        return success(insurancePlanService.selectDistinctPlanNamesWithIds());
    }
    /**
     * 导出参保方案列表
     */
    @PreAuthorize("@ss.hasPermi('cbfa:cbfa:export')")
    @Log(title = "参保方案", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, InsurancePlan insurancePlan)
    {
        List<InsurancePlan> list = insurancePlanService.selectInsurancePlanList(insurancePlan);
        ExcelUtil<InsurancePlan> util = new ExcelUtil<InsurancePlan>(InsurancePlan.class);
        util.exportExcel(response, list, "参保方案数据");
    }

    /**
     * 获取参保方案详细信息
     */
    @PreAuthorize("@ss.hasPermi('cbfa:cbfa:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(insurancePlanService.selectInsurancePlanById(id));
    }

    /**
     * 新增参保方案
     */
    @PreAuthorize("@ss.hasPermi('cbfa:cbfa:add')")
    @Log(title = "参保方案", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody InsurancePlan insurancePlan)
    {
        return toAjax(insurancePlanService.insertInsurancePlan(insurancePlan));
    }

    /**
     * 修改参保方案
     */
    @PreAuthorize("@ss.hasPermi('cbfa:cbfa:edit')")
    @Log(title = "参保方案", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody InsurancePlan insurancePlan)
    {
        return toAjax(insurancePlanService.updateInsurancePlan(insurancePlan));
    }

    /**
     * 删除参保方案
     */
    @PreAuthorize("@ss.hasPermi('cbfa:cbfa:remove')")
    @Log(title = "参保方案", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(insurancePlanService.deleteInsurancePlanByIds(ids));
    }
    @PreAuthorize("@ss.hasPermi('cbfa:cbfa:query')")
    @GetMapping("/insuranceTypes/{name}")
    public AjaxResult getDetailsByName(@PathVariable String name) {
        return success(insurancePlanService.getInsuranceDetailsByName(name));
    }

}
